package com.longyou.comm.conntroller;

import static com.longyou.comm.service.IValidateCodeGenerateService.VALIDATE_CODE_BEAN_PREFIX;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.longyou.comm.service.IValidateCodeGenerateService;
import com.unknow.first.geetest.config.GeetestConfig;
import com.unknow.first.geetest.feign.GeetestFeignClient;
import com.unknow.first.geetest.vo.GeetestQueryParamVO;
import com.unknow.first.geetest.vo.GeetestValidateResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.cloud.exception.BusinessException;
import org.cloud.utils.SpringContextUtil;
import org.cloud.vo.CommonApiResult;
import org.cloud.vo.ValidateCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/verify")
@Api(value = "前端:验证码接口", tags = "前端:验证码接口")
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class UserVerifyCodeController {

    /**
     * @param generateType 1:图形验证 2:手机验证
     * @return
     */

    @GetMapping("/generate/{generateType}")
    @ApiOperation("获取验证码")
    public CommonApiResult<ValidateCodeVO> generateVerifyCode(
        @ApiParam(value = "验证类型", defaultValue = "1") @PathVariable("generateType") Integer generateType,
        @ApiParam(value = "请求参数", required = false) @RequestParam Map<String, Object> requestParams) throws BusinessException {
        IValidateCodeGenerateService validateCodeGenerateService = SpringContextUtil.getBean(VALIDATE_CODE_BEAN_PREFIX + generateType);
        return CommonApiResult.createSuccessResult(validateCodeGenerateService.generate(requestParams));
    }

    private final GeetestConfig geetestConfig;
    private final GeetestFeignClient feignClient;

    @GetMapping("/geetest/captchaId")
    @ApiOperation("获取geetest配置信息")
    public CommonApiResult<GeetestConfig> geetestCaptchaId() {
//        Assert.isTrue(geetestConfig.getEnabled(), "system.error.geetest.disabled");
        GeetestConfig clone = BeanUtil.toBean(geetestConfig, GeetestConfig.class, CopyOptions.create().setIgnoreProperties("captchaKey"));
        return CommonApiResult.createSuccessResult(clone);
    }

    /**
     * @param
     * @return
     */

    @PostMapping("/geetest/verify")
    @ApiOperation("geetest验证（测试用）")
    public GeetestValidateResultVO geetestVerify(@RequestBody GeetestQueryParamVO requestParams) throws BusinessException {
        return feignClient.geetestVerify(requestParams);
    }
}
